<%-- 
    Document   : profile
    Created on : Apr 24, 2012, 8:02:21 PM
    Author     : Reinhard
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="/connect.jspf"%>
<%@include file="/header.jsp"%>
<%@page import="model.PostModel, model.TagModel, model.UserModel, model.AchievementModel, model.ListContentModel" %>

<%  
  String username, full_name, email, gender, birth, avatar, about_me, post_count, comment_count;
  username = full_name = email = gender = birth = avatar = about_me = post_count = comment_count = "";
  
  String profile_id = "";
  if (request.getParameter("id") != null) {
    profile_id = request.getParameter("id");
    UserModel um = new UserModel(con);
    ResultSet rs = um.getUserInfo(profile_id);
    
    boolean dapat = false;
    while (rs.next()) {
      dapat = true;
      username = rs.getString("username");
      full_name = rs.getString("full_name");
      email = rs.getString("email");
      gender = rs.getString("gender");
      birth = rs.getString("birth");
      avatar = rs.getString("avatar");
      about_me = rs.getString("about_me");
      post_count = rs.getString("post_count");
      comment_count = rs.getString("comment_count");
    }
%>

<% if (dapat) {    %>

<div id="bodycontent">
  <div class="contentcontainer">
    <div class="title-text">
      <h1>Profile Information</h1>
    </div>		
    <div class="contentbox">
      <div id="profile_id">Username: <%= username%></div>
      <div id="profile_fullname">Full name: <%= full_name%></div>
      <div id="profile_avatar">Avatar: <img class="avatar" src="avatar/<%= avatar%>" alt="avatar"/></div>
      <div id="profile_email">Email: <%= email%></div>
      <div id="profile_gender">Gender: <%= gender%></div>
      <div id="profile_about"> About Me: <%= about_me%></div>
      <div id="profile_achieve">Achievement:
        <ul>
          <%            
            AchievementModel am = new AchievementModel(con);
            ResultSet res = am.getAllAchievementAchieved(profile_id);
            ResultSet res2 = am.getAllAchievement();
            while (res2.next()) {
              out.println(res2.getString("name") + " : " + res2.getString("description"));
              boolean achieved = false;
              ResultSet res3 = am.getAllAchievementAchieved(profile_id);
              while (res3.next()) {
                if (res3.getString("achievement_id").equals(res2.getString("ID"))) {
                  out.println("<span class='achievement-achieved-check'> achieved! </span>");
                  achieved = true;
                  break;
                }
              }
              out.println("<br />");
            }
          %>      
        </ul>
      </div>
      <div id="profile_commentcount">Comment count: <%= comment_count %></div>
      <div id="profile_contentcount">Content count: <%= post_count %></div>
      <%        
        out.println("<script type='text/javascript' src='profile.js'></script>");
        if (session.getAttribute("userid") != null && session.getAttribute("userid").equals(profile_id)) {
          out.println("<h1>Edit profile</h1>");
          out.println("<form method='post' action='edit_profile.jsp' enctype='multipart/form-data'>");
          out.println("New Avatar: <input type='file' name='avatar' /> <br />");
          out.println("New Email: <input type='text' name='email' /> <br />");
          out.println("New About Me: <br/><textarea rets='5' cols='35' name='about'></textarea> <br />");
          out.println("<input type='submit' value='Done Editing' name='mit'/>");
          out.println("</form>");
        }
      %>
    </div>
  </div>

  <div class = "contentcontainer">
    <div class="title-text">
      <h1>Content Posted</h1>
    </div>
    <%
      ListContentModel lm = new ListContentModel(con);
      String contentModel = lm.listPost("SELECT * from post INNER JOIN user WHERE post.poster_id=user.ID", Integer.parseInt(profile_id));
      out.println(contentModel);
    %>
            
    <%-- <?php
    $pm = new Post_Model();
    $res = $pm->getUserPosts($profile_id);
    convertQuery($res, $profile_id, "id");
    ?> --%>
  </div>
</div>

}
<%      
    } else {
      out.println("<div class='contentbox'>");
      out.println("<p style='color:red' align='center'><b>No user with that ID</b></p>");
      out.println("</div>");
    }
  } else {
    pageContext.forward("index.jsp");
  }
%>


<%@include file="/footer.jsp"%>
